<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:exsl="http://exslt.org/common" version="1.0">

    <xsl:template name="appendix.titlepage.recto">
        <fo:block xsl:use-attribute-sets="appendix.titlepage.recto.style"
            margin-left="{$title.margin.left}" font-size="24.8832pt" font-family="{$title.fontset}"
            font-weight="bold">
            <xsl:call-template name="component.title.chapterlike">
                <xsl:with-param name="node" select="ancestor-or-self::appendix[1]"/>
            </xsl:call-template>
        </fo:block>
    </xsl:template>

    <xsl:template name="article.titlepage">
        <xsl:variable name="recto.content">
            <xsl:call-template name="article.titlepage.before.recto"/>
            <xsl:call-template name="article.titlepage.recto"/>
        </xsl:variable>
        <xsl:variable name="recto.elements.count">
            <xsl:choose>
                <xsl:when test="function-available('exsl:node-set')">
                    <xsl:value-of select="count(exsl:node-set($recto.content)/*)"/>
                </xsl:when>
                <xsl:when
                    test="contains(system-property('xsl:vendor'), 'Apache Software Foundation')">
                    <!--Xalan quirk-->
                    <xsl:value-of select="count(exsl:node-set($recto.content)/*)"/>
                </xsl:when>
                <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="verso.content">
            <xsl:call-template name="article.titlepage.before.verso"/>
            <xsl:call-template name="article.titlepage.verso"/>
        </xsl:variable>
        <xsl:variable name="verso.elements.count">
            <xsl:choose>
                <xsl:when test="function-available('exsl:node-set')">
                    <xsl:value-of select="count(exsl:node-set($verso.content)/*)"/>
                </xsl:when>
                <xsl:when
                    test="contains(system-property('xsl:vendor'), 'Apache Software Foundation')">
                    <!--Xalan quirk-->
                    <xsl:value-of select="count(exsl:node-set($verso.content)/*)"/>
                </xsl:when>
                <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="(normalize-space($recto.content) != '') or ($recto.elements.count &gt; 0)">
            <fo:block>
                <xsl:comment>recto</xsl:comment>
                <xsl:copy-of select="$recto.content"/>
            </fo:block>
        </xsl:if>
        <xsl:if test="(normalize-space($verso.content) != '') or ($verso.elements.count &gt; 0)">
            <fo:block>
                <xsl:comment>verso</xsl:comment>
                <xsl:copy-of select="$verso.content"/>
            </fo:block>
        </xsl:if>
        <xsl:call-template name="article.titlepage.separator"/>
    </xsl:template>

    <xsl:template name="article.titlepage.recto">
        <xsl:param name="article.cover.recto.content">
            <xsl:element name="fo:block" use-attribute-sets="article.titlepage.recto.style">
                <xsl:choose>
                    <!-- productname -->
                    <xsl:when test="articleinfo/productname">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="articleinfo/productname"/>
                    </xsl:when>
                    <xsl:when test="artheader/productname">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="artheader/productname"/>
                    </xsl:when>
                    <xsl:when test="info/productname">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="info/productname"/>
                    </xsl:when>
                    <xsl:when test="productname">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="productname"/>
                    </xsl:when>
                </xsl:choose>
                <xsl:choose>
                    <!-- productnumber -->
                    <xsl:when test="articleinfo/productnumber">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="articleinfo/productnumber"/>
                    </xsl:when>
                    <xsl:when test="artheader/productnumber">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="artheader/productnumber"/>
                    </xsl:when>
                    <xsl:when test="info/productnumber">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="info/productnumber"/>
                    </xsl:when>
                    <xsl:when test="productnumber">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="productnumber"/>
                    </xsl:when>
                </xsl:choose>
                <xsl:choose>
                    <!-- title -->
                    <xsl:when test="articleinfo/title">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="articleinfo/title"/>
                    </xsl:when>
                    <xsl:when test="artheader/title">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="artheader/title"/>
                    </xsl:when>
                    <xsl:when test="info/title">
                        <xsl:apply-templates mode="article.titlepage.recto.mode" select="info/title"
                        />
                    </xsl:when>
                    <xsl:when test="title">
                        <xsl:apply-templates mode="article.titlepage.recto.mode" select="title"/>
                    </xsl:when>
                </xsl:choose>
                <xsl:choose>
                    <!-- edition -->
                    <xsl:when test="articleinfo/edition">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="articleinfo/edition"/>
                    </xsl:when>
                    <xsl:when test="artheader/edition">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="artheader/edition"/>
                    </xsl:when>
                    <xsl:when test="info/edition">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="info/edition"/>
                    </xsl:when>
                    <xsl:when test="edition">
                        <xsl:apply-templates mode="article.titlepage.recto.mode" select="edition"/>
                    </xsl:when>
                </xsl:choose>
                <xsl:choose>
                    <!-- date -->
                    <xsl:when test="articleinfo/date">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="articleinfo/date"/>
                    </xsl:when>
                    <xsl:when test="artheader/date">
                        <xsl:apply-templates mode="article.titlepage.recto.mode"
                            select="artheader/date"/>
                    </xsl:when>
                    <xsl:when test="info/date">
                        <xsl:apply-templates mode="article.titlepage.recto.mode" select="info/date"
                        />
                    </xsl:when>
                    <xsl:when test="date">
                        <xsl:apply-templates mode="article.titlepage.recto.mode" select="subtitle"/>
                    </xsl:when>
                </xsl:choose>
            </xsl:element>
        </xsl:param>
        <xsl:choose>
            <xsl:when test="ancestor::book">
                <!-- same as chapter recto -->
                <fo:block xsl:use-attribute-sets="chapter.titlepage.recto.style"
                    margin-left="{$title.margin.left}" font-size="24.8832pt"
                    font-family="{$title.fontset}" font-weight="bold">
                    <xsl:call-template name="component.title.chapterlike">
                        <xsl:with-param name="node" select="ancestor-or-self::article[1]"/>
                    </xsl:call-template>
                </fo:block>
            </xsl:when>

            <xsl:when test="/article">
                <!-- same as book recto -->
                <xsl:call-template name="custom.book.cover.recto">
                    <xsl:with-param name="custom.book.cover.recto.content"
                        select="$article.cover.recto.content"/>
                </xsl:call-template>
            </xsl:when>

            <xsl:otherwise/>

        </xsl:choose>
    </xsl:template>

    <xsl:template name="article.titlepage.before.verso">
        <xsl:if test="/article">
            <!-- standalone article only-->
            <fo:block break-before="page">
                <xsl:comment>page breaker</xsl:comment>
            </fo:block>
        </xsl:if>
    </xsl:template>

    <xsl:template name="article.titlepage.separator">
        <xsl:if test="/article">
            <!-- standalone article only-->
            <fo:block break-after="page">
                <xsl:comment>page breaker</xsl:comment>
            </fo:block>
        </xsl:if>
    </xsl:template>

    <xsl:template name="article.titlepage.verso">
        <xsl:choose>
            <xsl:when test="ancestor::book">
                <!-- same as chapter verso -->
            </xsl:when>

            <xsl:when test="/article">

                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="articleinfo/releaseinfo"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="artheader/releaseinfo"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode" select="info/releaseinfo"/>

                <xsl:if test="$show.revhistory != '0'">
                    <xsl:apply-templates mode="article.titlepage.verso.mode"
                        select="articleinfo/revhistory"/>
                    <xsl:apply-templates mode="article.titlepage.verso.mode"
                        select="artheader/revhistory"/>
                    <xsl:apply-templates mode="article.titlepage.verso.mode"
                        select="info/revhistory"/>
                </xsl:if>

                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="articleinfo/copyright"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="artheader/copyright"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode" select="info/copyright"/>

                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="articleinfo/legalnotice"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode"
                    select="artheader/legalnotice"/>
                <xsl:apply-templates mode="article.titlepage.verso.mode" select="info/legalnotice"/>

            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="book.titlepage.recto">
        <xsl:param name="book.cover.recto.content">
            <xsl:element name="fo:block" use-attribute-sets="book.titlepage.recto.style">

                <xsl:choose>
                    <xsl:when test="bookinfo/productname">
                        <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                            select="bookinfo/productname"/>
                    </xsl:when>

                    <xsl:when test="info/productname">
                        <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                            select="info/productname"/>
                    </xsl:when>
                </xsl:choose>

                <xsl:choose>
                    <xsl:when test="bookinfo/productnumber">
                        <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                            select="bookinfo/productnumber"/>
                    </xsl:when>

                    <xsl:when test="info/productnumber">
                        <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                            select="info/productnumber"/>
                    </xsl:when>
                </xsl:choose>

                <xsl:element name="fo:block">
                    <xsl:attribute name="space-before">0.2in</xsl:attribute>
                    <xsl:choose>
                        <xsl:when test="bookinfo/title">
                            <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                                select="bookinfo/title"/>
                        </xsl:when>

                        <xsl:when test="info/title">
                            <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                                select="info/title"/>
                        </xsl:when>

                        <xsl:when test="title">
                            <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                                select="title"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:element>

                <xsl:element name="fo:block">
                    <xsl:attribute name="font-size">0.8em</xsl:attribute>

                    <fo:block>
                        <xsl:value-of select="normalize-space($doc.edition)"/>
                    </fo:block>

                    <xsl:choose>
                        <xsl:when test="bookinfo/date">
                            <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                                select="bookinfo/date"/>
                        </xsl:when>

                        <xsl:when test="info/date">
                            <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
                                select="info/date"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:element>
            </xsl:element>
        </xsl:param>

        <xsl:call-template name="custom.book.cover.recto">
            <xsl:with-param name="custom.book.cover.recto.content"
                select="$book.cover.recto.content"/>
        </xsl:call-template>

    </xsl:template>

    <xsl:template name="book.titlepage.verso">
        <!-- FIXME: same result expected for /article too. make consistant, then REUSE! -->
        <xsl:element name="fo:block-container" use-attribute-sets="rule.after.title.properties">
            <xsl:attribute name="space-after.minimum">1em</xsl:attribute>
            <xsl:choose>
                <xsl:when test="bookinfo/title">
                    <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                        select="bookinfo/title"/>
                </xsl:when>
                <xsl:when test="info/title">
                    <xsl:apply-templates mode="book.titlepage.verso.auto.mode" select="info/title"/>
                </xsl:when>
                <xsl:when test="title">
                    <xsl:apply-templates mode="book.titlepage.verso.auto.mode" select="title"/>
                </xsl:when>
            </xsl:choose>
        </xsl:element>

        <xsl:element name="fo:block-container">
            <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                select="bookinfo/releaseinfo|info/releaseinfo"/>
            <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                select="bookinfo/date|info/date"/>
            <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                select="bookinfo/abstract|info/abstract"/>
        </xsl:element>

        <xsl:if test="$show.revhistory != '0'">
            <xsl:element name="fo:block-container">
                <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                    select="bookinfo/revhistory"/>
                <xsl:apply-templates mode="book.titlepage.verso.auto.mode" select="info/revhistory"
                />
            </xsl:element>
        </xsl:if>

        <xsl:element name="fo:block-container">
            <xsl:attribute name="display-align">after</xsl:attribute>
            <xsl:attribute name="block-progression-dimension">100%</xsl:attribute>
            <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                select="bookinfo/copyright|info/copyright"/>
            <xsl:apply-templates mode="book.titlepage.verso.auto.mode"
                select="bookinfo/legalnotice|info/legalnotice"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="chapter.titlepage.recto">
        <fo:block xsl:use-attribute-sets="chapter.titlepage.recto.style"
            margin-left="{$title.margin.left}" font-size="24.8832pt" font-family="{$title.fontset}"
            font-weight="bold">
            <xsl:call-template name="component.title.chapterlike">
                <xsl:with-param name="node" select="ancestor-or-self::chapter[1]"/>
            </xsl:call-template>
        </fo:block>
    </xsl:template>

    <xsl:template name="glossary.titlepage.recto">
        <fo:block xsl:use-attribute-sets="glossary.titlepage.recto.style"
            margin-left="{$title.margin.left}" font-size="24.8832pt" font-family="{$title.fontset}"
            font-weight="bold">
            <xsl:call-template name="component.title.chapterlike">
                <xsl:with-param name="node" select="ancestor-or-self::glossary[1]"/>
            </xsl:call-template>
        </fo:block>
        <xsl:choose>
            <xsl:when test="glossaryinfo/subtitle">
                <xsl:apply-templates mode="glossary.titlepage.recto.auto.mode"
                    select="glossaryinfo/subtitle"/>
            </xsl:when>
            <xsl:when test="docinfo/subtitle">
                <xsl:apply-templates mode="glossary.titlepage.recto.auto.mode"
                    select="docinfo/subtitle"/>
            </xsl:when>
            <xsl:when test="info/subtitle">
                <xsl:apply-templates mode="glossary.titlepage.recto.auto.mode"
                    select="info/subtitle"/>
            </xsl:when>
            <xsl:when test="subtitle">
                <xsl:apply-templates mode="glossary.titlepage.recto.auto.mode" select="subtitle"/>
            </xsl:when>
        </xsl:choose>

    </xsl:template>


    <xsl:template name="section.titlepage.recto">

        <xsl:apply-templates mode="section.titlepage.recto.auto.mode"
            select="
      sectioninfo/title|info/title|title"/>

        <xsl:apply-templates mode="section.titlepage.recto.auto.mode"
            select="
      sectioninfo/subtitle|info/subtitle|subtitle"/>

        <xsl:apply-templates mode="section.titlepage.recto.auto.mode"
            select="
    sectioninfo/abstract|info/abstract"/>

    </xsl:template>

    <xsl:template name="custom.book.cover.recto">
        <!-- la page de couverture applique plusieurs modèles -->
        <xsl:param name="custom.book.cover.recto.content"/>
        <!-- 1. logo -->
        <xsl:call-template name="company.logo">
            <xsl:with-param name="size">1.50in</xsl:with-param>
        </xsl:call-template>
        <!-- 2. image de couverture (ligne produit) -->
        <xsl:call-template name="custom.book.cover.image"/>
        <!-- 3. conteneur pour le texte et les informations relatives au document-->
        <xsl:element name="fo:block-container">
            <xsl:attribute name="absolute-position">absolute</xsl:attribute>
            <xsl:attribute name="top">2in</xsl:attribute>
            <xsl:copy-of select="$custom.book.cover.recto.content"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="custom.book.cover.image">
        <xsl:element name="fo:block-container">
            <xsl:attribute name="absolute-position">absolute</xsl:attribute>
            <xsl:attribute name="top">65mm</xsl:attribute>
            <xsl:attribute name="left">
                <xsl:value-of select="concat('-',$page.margin.inner)"/>
            </xsl:attribute>
            <xsl:element name="fo:block">
                <xsl:element name="fo:external-graphic">
                    <xsl:attribute name="src">
                        <xsl:text>url('</xsl:text>
                        <xsl:value-of select="$doc.product-cover"/>
                        <xsl:text>')</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="content-width">
                        <xsl:value-of select="$page.width"/>
                    </xsl:attribute>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template name="index.titlepage.recto">
        <fo:block xsl:use-attribute-sets="index.titlepage.recto.style" margin-left="0pt"
            font-size="24.8832pt" font-family="{$title.fontset}" font-weight="bold">
            <xsl:call-template name="component.title">
                <xsl:with-param name="node" select="ancestor-or-self::index[1]"/>
                <xsl:with-param name="pagewide" select="1"/>
            </xsl:call-template>
        </fo:block>
        <xsl:choose>
            <xsl:when test="indexinfo/subtitle">
                <xsl:apply-templates mode="index.titlepage.recto.auto.mode"
                    select="indexinfo/subtitle"/>
            </xsl:when>
            <xsl:when test="docinfo/subtitle">
                <xsl:apply-templates mode="index.titlepage.recto.auto.mode"
                    select="docinfo/subtitle"/>
            </xsl:when>
            <xsl:when test="info/subtitle">
                <xsl:apply-templates mode="index.titlepage.recto.auto.mode" select="info/subtitle"/>
            </xsl:when>
            <xsl:when test="subtitle">
                <xsl:apply-templates mode="index.titlepage.recto.auto.mode" select="subtitle"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="preface.titlepage.recto">
        <fo:block xsl:use-attribute-sets="preface.titlepage.recto.style"
            margin-left="{$title.margin.left}" font-size="24.8832pt" font-family="{$title.fontset}"
            font-weight="bold">
            <xsl:call-template name="component.title.chapterlike">
                <xsl:with-param name="node" select="ancestor-or-self::preface[1]"/>
            </xsl:call-template>
        </fo:block>
    </xsl:template>

    <xsl:template match="title" mode="article.titlepage.verso.mode">
        <xsl:element name="fo:block" use-attribute-sets="article.titlepage.verso.style">
            <xsl:apply-templates select="." mode="titlepage.mode"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
